package jsyf.oa.web.psi;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.psi.InventoryFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Maqiuqiu on 2018/10/17.
 */
@Api(value = "进销存-库存接口", description = "进销存-库存接口")
@Controller
@RequestMapping("/inventory")
public class InventoryController {
    @Resource
    private InventoryFacade inventoryFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.inventoryFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = inventoryFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getInventoryList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询库存列表",
            notes = "查询库存列表",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getInventoryList(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.inventoryFacade.getInventoryList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getOutRepoInfo.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询出庫",
            notes = "查询出庫",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getOutRepoInfo(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.inventoryFacade.getOutRepoInfo(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getInventoryGoodsInfoById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据id获取申购信息",
            notes = "根据id获取申购信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:''}",dataType = "String", paramType = "body")
    public void getInventoryGoodsInfoById(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.inventoryFacade.getInventoryGoodsInfoById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = inventoryFacade.applyAudit(jsonData);
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        //sendMessage(result.getString("message"));
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }


}
